<?php

class Install_Form_Setup_Database extends Nuo_Form
{
    public function init()
    {
        $this -> addElement('text', 'host', array(
            'label' => 'host',
            'required' => true,
            'class' => 'text long-text',
            'value' => 'localhost',
        ));

        $this -> addElement('text', 'port', array(
            'label' => 'port',
            'required' => true,
            'class' => 'text long-text',
            'value' => '3306',
            'validators' => array('Int'),
        ));

        $this -> addElement('text', 'dbname', array(
            'label' => 'database',
            'required' => true,
            'class' => 'text long-text',
            'value' => '',
        ));

        $this -> addElement('text', 'username', array(
            'label' => 'username',
            'required' => true,
            'class' => 'text long-text',
            'value' => '',
        ));

        $this -> addElement('text', 'password', array(
            'label' => 'password',
            'required' => true,
            'class' => 'text long-text',
            'value' => '',
        ));

        $this -> addElement('button', '_submit', array(
            'label' => 'continue',
            'class' => 'submit',
            'type' => 'submit',
            'decorators' => array('ViewHelper')
        ));

        $this -> addElement('button', '_refresh', array(
            'label' => 'refresh',
            'class' => 'submit',
            'type' => 'submit',
            'decorators' => array('ViewHelper')
        ));

        $this -> addDisplayGroup(array(
            '_submit',
            '_refresh'
        ), '_buttons');

    }

    public function isValid($values)
    {
        $valid = parent::isValid($values);

        if (!$valid)
        {
            return $valid;
        }

        $db = Zend_Db::factory('pdo_mysql', $values);
        
        try
        {
            $status = $db -> query("SET time_zone = '+0:00'");
        }
        catch(Exception $e)
        {
            $this->addMessage($e->getMessage());
            return false;
        }
        return true;
    }

}
